#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/29 21:01
import os
from typing import List

import requests
from copy import deepcopy

from config.config import config
from domain.report.common.period_enum import PeriodEnum
from infrastructure.util.dateutils import get_current_year, get_dateStr_from_dateTime, today, DateFormat

report_base_path = config.get("data", "report_pdf")

URL_SSE = "http://www.sse.com.cn/disclosure/listedinfo/announcement/"
# 股票
URL_SSE_STOCK = "http://www.sse.com.cn/js/common/ssesuggestdata.js"
# 基金
URL_SSE_FUND = "http://www.sse.com.cn/js/common/ssesuggestfunddata.js"
# E债券
URL_SSE_EBOND = "http://www.sse.com.cn/js/common/ssesuggestEbonddata.js"
# T债券
URL_SSE_TBOND = "http://www.sse.com.cn/js/common/ssesuggestTbonddata.js"
# 查询
URL_QUERY_COMPANY = "http://query.sse.com.cn/security/stock/queryCompanyBulletin.do"

URL_PDF = "http://static.sse.com.cn"

# 报告类型
REPORT_TYPE = {
    '全部': ('ALL', ''),
    '定期公告': ('ALL', 'DQBG'),
    '年报': ('YEARLY', 'DQBG'),
    '第一季度季报': ('QUATER1', 'DQBG'),
    '半年报': ('QUATER2', 'DQBG'),
    '第三季度季报': ('QUATER3', 'DQBG'),
    '临时公告': ('ALL', 'LSGG'),
    '上市公司章程': ('SHGSZC', 'LSGG'),
    '发行上市公告': ('FXSSGG', 'LSGG'),
    '公司治理': ('GSZL', 'LSGG'),
    '股东大会会议': ('GDDH', 'LSGG'),
    'IPO公司公告': ('IPOGG', 'LSGG'),
    '其他': ('QT', 'LSGG'),
}

PERIOD_REPORTTYPE = {
    PeriodEnum.first_quarter.name: "第一季度季报",
    PeriodEnum.half_year.name: "半年报",
    PeriodEnum.three_quarter.name: "第三季度季报",
    PeriodEnum.year.name: "年报",
    PeriodEnum.all.name: "全部",
}

# 证券类型
SECURITY_TYPE = {
    '全部': '0101,120100,020100,020200,120200',
    '主板': '0101',
    '科创板': '120100,020100,020200,120200',
}

HEADER = {
    'Referer': URL_SSE,
    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
}

URL_PARAM = {
    # 是否分页
    'isPagination': 'false',
    'productId': '600000',
    # 关键字
    'keyWord': '',
    'securityType': SECURITY_TYPE['全部'],
    'reportType2': 'DQBG',
    'reportType': 'YEARLY',
    'beginDate': '2016-07-17',
    'endDate': '2019-07-17',
}


def _get_all_codes(url):
    res = requests.get(url)
    content = res.content.decode()
    tmp = content.split('_t.push({val:"')
    code, name, pinyin = [], [], []
    for i in tmp[1:]:
        item = i.split('"')
        code.append(item[0])
        name.append(item[2])
        pinyin.append(item[4])
    # print(code)
    return code, name, pinyin


def _get_pdf_url(code, begin_date, end_date, security_type='全部', report_type='年报'):
    url_param = deepcopy(URL_PARAM)
    url_param['productId'] = code
    url_param['securityType'] = SECURITY_TYPE[security_type]
    url_param['reportType2'] = REPORT_TYPE[report_type][1]
    url_param['reportType'] = REPORT_TYPE[report_type][0]
    url_param['beginDate'] = begin_date
    url_param['endDate'] = end_date
    result = requests.get(URL_QUERY_COMPANY, params=url_param, headers=HEADER).json()['result']
    return [(URL_PDF + i['URL'], i['BULLETIN_TYPE'], i['BULLETIN_YEAR'], i['SSEDATE']) for i in result]


def _save_pdf(code, pdf_title_urls, path=report_base_path) -> List[str]:
    file_path = os.path.join(path, code)
    saved_names = list()
    if not os.path.isdir(file_path):
        os.makedirs(file_path)
    for url, r_type, year, date in pdf_title_urls:
        date = ''.join(date.split('-'))
        file_name = '_'.join([code, r_type, year, date]) + '.pdf'
        file_full_name = os.path.join(file_path, file_name)
        if not os.path.exists(file_full_name):
            # print(file_full_name)
            rs = requests.get(url, stream=True)
            with open(file_full_name, "wb") as fp:
                for chunk in rs.iter_content(chunk_size=10240):
                    if chunk:
                        fp.write(chunk)
        saved_names.append(file_name)
    return saved_names


def download_report(stock_id:str, year:int, period:PeriodEnum=PeriodEnum.year) -> List[str]:
    if stock_id is not None or len(stock_id) == 0 or year < 1990 or year > get_current_year():
        return list()
    if period is None:
        period = PeriodEnum.all
    stock_id = stock_id.split(".")[0]
    begin_date = f"{year}-01-01"
    end_date = get_dateStr_from_dateTime(today(), dateFormat=DateFormat.Y_m_d)
    pdf_urls = _get_pdf_url(stock_id, begin_date, end_date, report_type=PERIOD_REPORTTYPE[period.name])
    saved_names = _save_pdf(stock_id, pdf_urls)
    return saved_names
